﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Dao;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.Metadata.Service
{
    public class MetaDataContentManager
    {
        //public void SaveMetaDataContent(IMetaDataContent metadataContent, string metadataType)
        //{
        //    var manager = MetaDataFactory.CreateRTPMetaDataManager(metadataType);
        //    if (manager == null) {
        //        throw new FasException(MetaDataExceptionCode.GetMetaDataInfoException,
        //                string.Format(MetaDataExceptionCode.GetMetaDataInfoExceptionMessage, metadataType + ": MetaDataManagerType为空"));
        //    }
        //    manager.Save(metadataContent);
        //}

        public IMetaDataContent GetMetaDataContent(string metaDataId, string metadataType)
        {
            var manager = MetaDataManagerFactory.CreateMetaDataManager(metadataType);
            if (manager == null)
            {
                throw new FasException(MetaDataExceptionCode.GetMetaDataInfoException,
                        string.Format(MetaDataExceptionCode.GetMetaDataInfoExceptionMessage, metadataType + ": MetaDataManagerType为空"));
            }
            return manager.GetMetaDataContent(metaDataId);
        }

        public string SerializeMetaData(IMetaData metaData)
        {
            var metadataType = metaData.MetaTypeID;

            var manager = MetaDataManagerFactory.CreateMetadataSerializer(metadataType);
            if (manager == null)
            {
                throw new FasException(MetaDataExceptionCode.GetMetaDataInfoException,
                        string.Format(MetaDataExceptionCode.GetMetaDataInfoExceptionMessage, metadataType + ": MetadataSerializerType为空"));
            }
            return manager.SerializeObject(metaData.Content, MetadataSerializeType.JSON);
        }
        public IMetaDataContent DeSerializeMetaData(IMetaData metaData)
        {
            var metadataType = metaData.MetaTypeID;

            var manager = MetaDataManagerFactory.CreateMetadataSerializer(metadataType);
            if (manager == null)
            {
                throw new FasException(MetaDataExceptionCode.GetMetaDataInfoException,
                        string.Format(MetaDataExceptionCode.GetMetaDataInfoExceptionMessage, metadataType + ": MetadataSerializerType为空"));
            }
            return manager.DeserializeObject(metaData.ContentJSON, MetadataSerializeType.JSON);
        }
    }
}
